## Primary DB server

resource "yandex_compute_instance" "db-1" {
  name = "db-1"

  resources {
    cores  = 2
    memory = 2
    core_fraction = 5
  }

  boot_disk {
    initialize_params {
      image_id = "fd8dt1r96q73kcsgmqb9" #centos-stream-8-v20230821
    }
  }

  secondary_disk {
    disk_id = yandex_compute_disk.db1-data.id
    auto_delete = true
  }

  network_interface {
    subnet_id = yandex_vpc_subnet.subnet-1.id
    nat       = true
  }

  metadata = {
    user-data = "${file("./cloud-init.yaml")}"
  }
  
  scheduling_policy {
    preemptible = true
  }
}


output "internal_ip_address_db_1" {
  value = yandex_compute_instance.db-1.network_interface.0.ip_address
}

output "external_ip_address_db_1" {
  value = yandex_compute_instance.db-1.network_interface.0.nat_ip_address
}



## Secondary DB server

resource "yandex_compute_instance" "db-2" {
  name = "db-2"

  resources {
    cores  = 2
    memory = 2
    core_fraction = 5
  }

  boot_disk {
    initialize_params {
      image_id = "fd8dt1r96q73kcsgmqb9" #centos-stream-8-v20230821
    }
  }

  secondary_disk {
    disk_id = yandex_compute_disk.db2-data.id
    auto_delete = true
  }

  network_interface {
    subnet_id = yandex_vpc_subnet.subnet-1.id
    nat       = true
  }

  metadata = {
    user-data = "${file("./cloud-init.yaml")}"
  }
  
  scheduling_policy {
    preemptible = true
  }
}


output "internal_ip_address_db_2" {
  value = yandex_compute_instance.db-2.network_interface.0.ip_address
}

output "external_ip_address_db_2" {
  value = yandex_compute_instance.db-2.network_interface.0.nat_ip_address
}

